﻿/*----------------------------------------------------------------
// Copyright (C) 2012 Q.FunnyFace
// 版权所有。 
//
// 文件名：LoginController.cs
// 文件功能描述：登录控制器
//
// 
// 创建标识：2012-10-26 zhangqiang
//
// 修改标识：
// 修改描述：
//
//
//----------------------------------------------------------------*/
using Common;
using DomainModel;
using IManager;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebHelper.SessionHandler;
using WebPlatform.FiltersAndSession;
using WebPlatform.Models;
using IDataAccess;
using Common.Extensions.Where;

namespace WebPlatform.Controllers
{

    /// <summary>
    /// 登录控制器
    /// </summary>
    public class LoginController : Controller
    {
        
        /// <summary>
        /// 登录页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            LoginVM loginVM = new LoginVM();

            loginVM.LoginName = ISHandler.GetSession<string>(LoginVM.LoginNameKey);

            loginVM.Indate = ISHandler.GetSession<string>(LoginVM.IndateKey);

            return View(loginVM);
        }
        

        /// <summary>
        /// 用户信息操作
        /// </summary>
        public IManagerUser IMUser { get; set; }


        /// <summary>
        /// 会话操作
        /// </summary>
        public ISessionHandler ISHandler { get; set; }


        /// <summary>
        /// 企业信息
        /// </summary>
        public IManagerCompany IMCompany { get; set; }


        /// <summary>
        /// 登录会话操作
        /// </summary>
        public Sessions Sessions { get; set; }


        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="loginInfo">用户输入信息</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Login(LoginVM loginInfo)
        {

            #region 验证登录输入


            if (string.IsNullOrEmpty(loginInfo.LoginName))
            {
                ModelState.AddModelError("LoginName", "请输入用户名");
                return View("Index", loginInfo);
            }

            if (string.IsNullOrEmpty(loginInfo.LoginPassword))
            {
                ModelState.AddModelError("LoginPassword", "请输入密码");
                return View("Index", loginInfo);
            }

            if (string.IsNullOrEmpty(loginInfo.LoginCode))
            {
                ModelState.AddModelError("LoginCode", "请输入验证码");
                return View("Index", loginInfo);
            }

            if (loginInfo.LoginCode != ISHandler.GetSession<string>(LoginVM.ValidateCodeKey))
            {
                ModelState.AddModelError("LoginCode", "验证码不正确");
                return View("Index", loginInfo);
            }


            #endregion

            var user = IMUser.LoginUser(loginInfo.LoginName, loginInfo.LoginPassword);

            if (user != null && user.UserType == 1)
            {

                #region 保留登录痕迹


                string indate = ISHandler.GetSession<string>(LoginVM.IndateKey);
                if (string.IsNullOrEmpty(indate) || indate != loginInfo.Indate)
                {
                    if (string.IsNullOrEmpty(loginInfo.Indate))
                    {
                        ISHandler.ClearSession(LoginVM.IndateKey);
                    }
                    else
                    {
                        var time = ExpirationDate(loginInfo.Indate);
                        ISHandler.CreateSession<string>(LoginVM.IndateKey, loginInfo.Indate, time);
                        ISHandler.CreateSession<string>(LoginVM.LoginNameKey, loginInfo.LoginName, time);
                    }
                }


                #endregion


                #region 会话保存


                UserSession uSession = new UserSession();
                uSession.LoginName = user.LoginName;
                uSession.UserID = user.UserID;
                uSession.UserName = user.UserName;
                uSession.UserType = user.UserType;
                uSession.CompanyId = user.CompanyID;
                uSession.Entry = "Back";
                Sessions.User = uSession;

                #endregion


                //清除验证码
                ISHandler.ClearSession(LoginVM.ValidateCodeKey);

                return RedirectToAction("Index", "Home");
            }
            else
            {
                ModelState.AddModelError("LoginCode", "用户名或密码不正确");
            }
            return View("Index", loginInfo);
        }


        /// <summary>
        /// 生成验证码图片
        /// </summary>
        /// <returns>图片文件</returns>
        public ActionResult GetValidateCode()
        {
            ValidateCode vCode = new ValidateCode();
            string code = vCode.CreateValidateCode(4);
            ISHandler.CreateSession<string>(LoginVM.ValidateCodeKey, code, 5);
            byte[] bytes = vCode.CreateValidateGraphic(code);
            return File(bytes, @"image/jpeg");
        }


        /// <summary>
        /// cookie过期时间，分钟
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        int ExpirationDate(string value)
        {
            var now = DateTime.Now;
            switch (value)
            {
                case "oneDay":
                    return 24 * 60;
                case "oneWeek":
                    return 24 * 60 * 7;
                default:
                    return 24 * 60 * (DaysOfMonth(now));
            }
        }


        /// <summary>
        /// 获取该日期中月份天数
        /// </summary>
        /// <param name="dateTime">日期</param>
        /// <returns>月份天数</returns>
        public int DaysOfMonth(DateTime dateTime)
        {
            return (new DateTime(dateTime.Year, dateTime.Month, 1).AddMonths(1) -
                new DateTime(dateTime.Year, dateTime.Month, 1)).Days;
        }


    }
}
